package com.dev.hikmachine.controller;


import cn.hutool.core.util.StrUtil;
import com.dev.hikmachine.annotation.CheckDeviceLogin;
import com.dev.hikmachine.entity.access.AccessPeople;
import com.dev.hikmachine.param.AccessControlUser;
import com.dev.hikmachine.rtsp.HikDevResponse;
import com.dev.hikmachine.rtsp.QueryRequest;
import com.dev.hikmachine.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wisnky
 * @date 2024-4-17 14:33
 */

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("api/accessControl")
public class AccessControlController {

    private final HikAccessControlService hikAccessControlService;

    private final HikDevService hikDevService;


    /**
     * 【卡】根据设备序列号deviceSn查询所有卡信息
     *
     * @param ipv4Address 设备IP
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @GetMapping("getAllCardInfo/{ipv4Address}")
    public HikDevResponse getAllCardInfo(@PathVariable String ipv4Address) {
        return this.hikAccessControlService.getAllCardInfo(ipv4Address);
    }

    /**
     * 【用户】根据设备ip查询所有用户信息
     *
     * @param ipv4Address  设备IP
     * @param employeeNos  用户id
     * @param queryRequest 分页参数: pageNum, pageSize
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @GetMapping("getAllUserInfo/{ipv4Address}")
    public HikDevResponse getAllUserInfo(@PathVariable String ipv4Address, String employeeNos, QueryRequest queryRequest) {
        String[] ids = {};
        if (StrUtil.isNotBlank(employeeNos)) {
            ids = employeeNos.split(",");
        }
        return this.hikAccessControlService.getAllUserInfo(ipv4Address, ids, queryRequest);
    }

    /**
     * 【用户】下发
     *
     * @param accessControlUser 门禁用户: ipv4Address, realName, int:employeeNo
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @PostMapping("addUser/{ipv4Address}")
    public HikDevResponse addUser(@PathVariable String ipv4Address, @RequestBody AccessControlUser accessControlUser) {
        return this.hikAccessControlService.addUser(ipv4Address, accessControlUser);
    }

    /**
     * 【用户】修改
     *
     * @param accessPeople 门禁用户: ip, realName, int:employeeNo
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @PutMapping("modifyUser/{ipv4Address}")
    public HikDevResponse modifyUser(@PathVariable String ipv4Address, @RequestBody AccessPeople accessPeople) {
        return this.hikAccessControlService.modifyUser(ipv4Address, accessPeople);
    }

    /**
     * 【用户】批量下发
     *
     * @param accessControlUserList 门禁用户列表: ip, realName, int:employeeNo
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @PostMapping("addMultiUser/{ipv4Address}")
    public HikDevResponse addMultiUser(@PathVariable String ipv4Address, @RequestBody List<AccessControlUser> accessControlUserList) {
        return this.hikAccessControlService.addMultiUser(ipv4Address, accessControlUserList);
    }

    /**
     * 【用户】下发人脸
     *
     * @param accessControlUser 门禁用户: ip, employeeNo, base64Pic
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @PutMapping("addUserFace/{ipv4Address}")
    public HikDevResponse addUserFace(@PathVariable String ipv4Address, @RequestBody AccessControlUser accessControlUser) {
        return this.hikAccessControlService.addUserFace(ipv4Address, accessControlUser);
    }

    /**
     * 【用户】批量下发人脸
     *
     * @param accessControlUserList 门禁用户列表: ip, employeeNo, base64Pic
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @PutMapping("addMultiUserFace/{ipv4Address}")
    public HikDevResponse addMultiUserFace(@PathVariable String ipv4Address, @RequestBody List<AccessControlUser> accessControlUserList) {
        return this.hikAccessControlService.addMultiUserFace(ipv4Address, accessControlUserList);
    }

    /**
     * 【用户】批量删除人脸
     *
     * @param ipv4Address 设备ip
     * @param employeeIds 多个用户工号，用逗号隔开
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @DeleteMapping("delMultiUserFace")
    public HikDevResponse delMultiUserFace(String ipv4Address, String employeeIds) {
        String[] ids = employeeIds.split(",");
        return this.hikAccessControlService.delMultiUserFace(ipv4Address, ids);
    }

    /**
     * 【用户】批量删除用户
     *
     * @param ipv4Address 设备ip
     * @param employeeIds 多个用户工号，用逗号隔开
     * @return HikDevResponse
     */
    @CheckDeviceLogin
    @DeleteMapping("delMultiUser")
    public HikDevResponse delMultiUser(String ipv4Address, String employeeIds) {
        String[] ids = employeeIds.split(",");
        return this.hikAccessControlService.delMultiUser(ipv4Address, ids);
    }

}
